<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>网络货运平台省运管数据控制台</title>
    <meta name="renderer" content="webkit">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="keywords" content="小镖人、社区、网络货运">
    <meta name="description" content="小镖人、社区、网络货运">
    <link rel="stylesheet" href="../../../xbr_layui_extend/layui-v2.6.8/css/layui.css">
</head>
<body>
<div class="layui-fluid" style="padding: 15px;">
    <!--表格部分-->
    <table class="layui-hide" id="accTable" lay-filter="accTable"></table>
    <div id="accTablePage"></div>
    <!--隐藏模板 -->
    <script type="text/html" id="bar">
        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="download">下载到本地</a>
    </script>
</div>

<script src="../../../xbr_layui_extend/layui-v2.6.8/layui.js"></script>
<script>
    layui.extend({
        admin: '/platform/xbr_layui_extend/admin' //集成路径
    }).use(["admin", 'table', 'laypage'], function () {
        const $ = layui.$, table = layui.table, admin = layui.admin, laypage = layui.laypage;
        //加载TABLE
        refTable(1, 10);

        function refTable(page, limit) {
            admin.req({
                url: "/platform/pab/getDownloadTaskPage",
                data: {page: page, limit: limit},
                done: function (res) {
                    if (res.data == null) return;
                    table.render({
                        elem: '#accTable',
                        cols: [[ //标题栏
                            {field: 'tradeSn', title: '下载流水号'},
                            {field: 'fileName', title: '文件名称'},
                            {field: 'randomPwd', title: '随机密码'},
                            {field: 'fileDesc', title: '状态描述', minWidth: 150},
                            {fixed: 'right', title: '操作', toolbar: '#bar', width: 120}
                        ]],
                        data: res.data.list
                    });
                    //总页数低于页码总数
                    laypage.render({
                        elem: 'accTablePage',
                        limit: limit,
                        curr: page,
                        limits: [10, 50, 100, 500],
                        count: res.data.total, //数据总数
                        jump: function (obj, first) {
                            if (first) return;
                            refTable(obj.curr, obj.limit)
                        }
                    });
                }
            });
        }

        //监听行工具事件
        table.on('tool(accTable)', function (obj) {
            const data = obj.data;
            if (obj.event === 'download') {
                if (data['downloadUrl'] == null) {
                    admin.req({
                        url: "/platform/pab/generateFileDownloadUrl",
                        data: {fileName: data.fileName},
                        done: function (res) {
                            download(data.fileName, res.data);
                        }
                    })
                } else {
                    download(data.fileName, data['downloadUrl']);
                }
            }
        });

        //建立下载
        function download(fileName, blobUrl) {
            const a = document.createElement('a');
            a.download = fileName;
            a.style.display = 'none';
            a.href = blobUrl;
            a.click();
            document.body.removeChild(a);
        }


    });
</script>
</body>
</html>